import UIKit

class MyMessageViewController: BaseViewController,beginRefresh {
    var collection : MineMessageCollectionView?
    var currentPage : Int = 1
    var noData : NoDataView?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.title = "我的消息"
        self.defaultBackButton()
        self.view.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        collection = MineMessageCollectionView.init(frame: CGRect.init(x: 0, y: 0, width: YXDeviceWidth, height: YXDeviceHeight - 64), delegate: self)
        self.view.addSubview(collection!)
        noData = NoDataView.init(frame: CGRect.init(x: 0, y: 0, width: YXDeviceWidth, height: YXDeviceHeight - 64))
        noData?.refreshBlock = {
            self.beginRefresh()
        }
        self.collection?.isHidden = true
        self.noData?.isHidden = true
        self.view.addSubview(noData!)
        beginRefresh()
    }
    
    func beginRefresh() {
        currentPage = 1
        Manager.shared().getWidthDrawRecord(page: String(currentPage), success: {(model) in
            if let mod = model as? MineCollectionMessageModel {
                if mod.sourceArr.count > 0 {
                    self.noData?.isHidden = true
                    self.collection?.isHidden = false
                    self.collection!.feedData(model: mod)
                } else {
                    self.noData?.isHidden = false
                    self.collection?.isHidden = true
                    self.noData?.endRefresh()
                }
            }
        }, fail: {(code) in
            self.noData?.endRefresh()
            self.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    
    func loadNext() {
        currentPage = currentPage + 1
        self.collection?.table?.startAnimation = true
        Manager.shared().getWidthDrawRecord(page: String(currentPage), success: {(model) in
            if let mod = model as? MineCollectionMessageModel {
                self.collection!.appendData(model: mod)
            }
        }, fail: {(code) in
            //alert
            self.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
